package com.example.blackbox.Fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.blackbox.Adapter.BannerAdapter;
import com.example.blackbox.Adapter.GameLAdapter;
import com.example.blackbox.Data.Banner;
import com.example.blackbox.Data.GameL;
import com.example.blackbox.Data.JsonTool;
import com.example.blackbox.Net.NetTool;
import com.example.blackbox.R;

import java.util.ArrayList;
import java.util.List;

public class GameFragment extends Fragment {
    View view;
    RecyclerView recyclerView;
    GameLAdapter gameLAdapter;
    Handler handlerGame,handlerBanner;
    String data,bannerData;
    ViewPager vp;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_gameku,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initUI();
        handlerGame=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                ArrayList<GameL> gameLS = JsonTool.getGameL(data);
                GridLayoutManager layoutManager=new GridLayoutManager(getActivity(),2);
                recyclerView.setLayoutManager(layoutManager);
                gameLAdapter=new GameLAdapter(gameLS,getActivity());
                recyclerView.setAdapter(gameLAdapter);
            }
        };


        handlerBanner=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                Banner banner = JsonTool.getBanner(bannerData);
                BannerAdapter bannerAdapter=new BannerAdapter();
                bannerAdapter.setData(banner.getBanner());
                vp.setAdapter(bannerAdapter);

            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                data = NetTool.getJsonString("http://10.0.2.2:8080/androids/json/gamel.json");
                Message m=handlerGame.obtainMessage();
                handlerGame.sendMessage(m);
            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                bannerData=NetTool.getJsonString("http://10.0.2.2:8080/androids/json/banner.json");
                Message m=handlerBanner.obtainMessage();
                handlerBanner.sendMessage(m);
            }
        }).start();

    }
    public void initUI(){
        recyclerView=getActivity().findViewById(R.id.gameku_recycler);
        vp=getActivity().findViewById(R.id.gameku_vp);
    }
}
